﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using 视图.Models;

namespace 视图.ViewComponents
{
    public class Blogs2ViewComponent : ViewComponent
    {
        //同步
        //public IViewComponentResult Invoke()
        //{
        //    var blogs = new List<Blog>()
        //    {
        //        new Blog(){ Id=1,Name="李四" },
        //        new Blog(){ Id=2,Name="张三" }
        //    };

        //    //指定视图名称的方式
        //    return View("blogs2", blogs);
        //}


        //异步
        public Task<IViewComponentResult> InvokeAsync(int? id,string title)
        {
            ViewBag.h1Name = title??"Blog2-1111";

            var blogs = new List<Blog>()
            {
                new Blog(){ Id=1,Name="李四" },
                new Blog(){ Id=2,Name="张三" }
            };

            if (id.HasValue)
            {
                blogs = blogs.Where(b=>b.Id==id).ToList();
            }

            //指定视图名称的方式
            return Task.FromResult((IViewComponentResult)View("blogs2", blogs));
        }
    }
}
